/**
 * Filename: SettingActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 2:36
 */
package com.github.postsanf.yinian.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.utils.ToastUtils;
import com.github.postsanf.yinian.widget.TipEditText;
import com.umeng.analytics.MobclickAgent;

public class TimeCardwordActivity extends Activity implements View.OnClickListener {
    private TextView tv_write_complete;
    private TextView tv_write_cancel;
    private InputMethodManager imm;
    private TipEditText et_write_word;
    private String time_card_word;
    private String time_card_to;
    private String time_card_edit_from;
    private String day_sign_content;
    private String day_sign_author;
    private String text_content = "";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // Translucent navigation bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setContentView(R.layout.yn_activity_voicecard_from);
        initView();
    }

    private void initView() {
        et_write_word = ((TipEditText) findViewById(R.id.et_write_word));
        tv_write_complete = (TextView) findViewById(R.id.tv_write_complete);
        tv_write_cancel = (TextView) findViewById(R.id.tv_write_cancel);
        findViewById(R.id.timecard_from_enty_scroll).setOnClickListener(this);
        tv_write_complete.setOnClickListener(this);
        tv_write_cancel.setOnClickListener(this);
        time_card_word = getIntent().getStringExtra("time_card_word");
        time_card_to = getIntent().getStringExtra("time_card_to");
        time_card_edit_from = getIntent().getStringExtra("time_card_edit_from");
        day_sign_content = getIntent().getStringExtra("day_sign_content");
        day_sign_author = getIntent().getStringExtra("day_sign_author");
        text_content = getIntent().getStringExtra("text_content");
        et_write_word.setText(text_content);
        if (TextUtils.equals(time_card_word, "time_card_word")) {
            et_write_word.setTxMaxCount(48);
            et_write_word.setFilters(new InputFilter[]{new InputFilter.LengthFilter(48)});
            et_write_word.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    if (et_write_word.length() >= 48) {
                        ToastUtils.showToast(TimeCardwordActivity.this, "最多输入48个字", 1000);
                    }
                }
            });
        }
        if (TextUtils.equals(time_card_edit_from, "time_card_edit_from") || TextUtils.equals(time_card_to, "time_card_to")) {
            et_write_word.setTxMaxCount(6);
            et_write_word.setSingleLine(true);
            et_write_word.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
            et_write_word.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    if (et_write_word.length() >= 6) {
                        ToastUtils.showToast(TimeCardwordActivity.this, "最多输入6个字", 1000);
                    }
                }
            });
        }
        if (TextUtils.equals(day_sign_author, "day_sign_author")) {
            et_write_word.setTxMaxCount(8);
            et_write_word.setSingleLine(true);
            et_write_word.setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
            et_write_word.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    if (et_write_word.length() >= 8) {
                        ToastUtils.showToast(TimeCardwordActivity.this, "最多输入8个字", 1000);
                    }
                }
            });
        }
        if (TextUtils.equals(day_sign_content, "day_sign_content")) {
            et_write_word.setTxMaxCount(-1);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_write_complete:
                Intent mIntent = new Intent(ACTIONs.actionUpdateFromText);
                if (TextUtils.equals(time_card_word, "time_card_word")) {
                    mIntent.putExtra("time_card_word", et_write_word.getText().toString());
                }
                if (TextUtils.equals(time_card_edit_from, "time_card_edit_from")) {
                    mIntent.putExtra("time_card_edit_from", et_write_word.getText().toString());
                }
                if (TextUtils.equals(time_card_to, "time_card_to")) {
                    mIntent.putExtra("time_card_to", et_write_word.getText().toString());
                }
                if (TextUtils.equals(day_sign_author, "day_sign_author")) {
                    mIntent.putExtra("day_sign_author", et_write_word.getText().toString());
                }
                if (TextUtils.equals(day_sign_content, "day_sign_content")) {
                    mIntent.putExtra("day_sign_content", et_write_word.getText().toString());
                }
                imm.hideSoftInputFromWindow(et_write_word.getWindowToken(), 0);
                sendBroadcast(mIntent);
                finish();
                break;
            case R.id.tv_write_cancel:
                imm.hideSoftInputFromWindow(et_write_word.getWindowToken(), 0);
                finish();
                break;
            case R.id.timecard_from_enty_scroll:
                Log.i("xjs","timecard_from_enty_scroll");
                Intent mIntent2 = new Intent(ACTIONs.actionUpdateFromTextCancel);
                mIntent2.putExtra("day_sign_content", et_write_word.getText().toString());
                sendBroadcast(mIntent2);
                imm.hideSoftInputFromWindow(et_write_word.getWindowToken(), 0);
                finish();
                break;
        }
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
